<?php

// Dependencies

require_once(PATH_LOOP . 'Model/CProfilingEvent.php');
require_once(PATH_LOOP . 'Model/CProfilingThread.php');


//

$profilingProcess = app()->getProfilingManager();


// Get all profiling threads

$profilingThread = new \Loop\CProfilingThread();
$currentProfilingThread = new \Loop\CProfilingThread();
$rawProfilingThreads = $profilingProcess->getRawProfilingThreads();


//

try
{
	$currentProfilingThread->loadSecure
	(
		fv('profilingThreadId'),
		fv('profilingThreadReference')
	);
}
catch (\Exception $e)
{
}

if
(
	($currentProfilingThread->isLoaded() === false) &&
	(count($rawProfilingThreads) > 0)
)
{
	$rawProfilingThread = array_shift($rawProfilingThreads);
	$currentProfilingThread->loadFromRawObject($rawProfilingThread);
	array_unshift($rawProfilingThreads, $rawProfilingThread);
}


// Get all profiling events for this session

$profilingEvent = new \Loop\CProfilingEvent();
$rawProfilingEvents = $currentProfilingThread->getRawProfilingEvents();


// Admin pages have a custom header/footer

pref('[Loop]/views/pathToSnippetFooter.html', '/[Loop]/Html/Snippet.Admin.Footer.html');
pref('[Loop]/views/pathToSnippetHeader.html', '/[Loop]/Html/Snippet.Admin.Header.html');


function getPhpInformations()
{
	//

	ob_start(); 
	phpinfo(-1);
	$rawResult = ob_get_clean();


	//

	$rawResult = preg_replace
	(
		array
		(
			'#^.*<body>(.*)</body>.*$#ms',
			'#<h2>PHP License</h2>.*$#ms',
			'#<h1>Configuration</h1>#',
			"#\r?\n#",
			"#</(h1|h2|h3|tr)>#",
			'# +<#',
			"#[ \t]+#", '#&nbsp;#',
			'#  +#',
			'# class=".*?"#',
			'%&#039;%',
			'#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a><h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
			'#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
			'#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
			"# +#", '#<tr>#', '#</tr>#'
		),
		array
		(
			'$1',
			'',
			'',
			'',
			"</\$1>\n",
			'<',
			' ',
			' ',
			' ',
			'',
			' ',
			'<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'."\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
			'<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
			'<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" .
			'<tr><td>Zend Egg</td><td>$1</td></tr>', ' ', '%S%', '%E%'
		),
		$rawResult
	);


	//

	$rawSections = explode('<h2>', strip_tags($rawResult, '<h2><th><td>'));
	unset($rawSections[0]);

	
	//

	$result = array();

	foreach ($rawSections as $rawSection)
	{
		//

		$sectionName = substr($rawSection, 0, strpos($rawSection, '</h2>'));
		$result[$sectionName] = array();

		
		//

		preg_match_all
		(
			'#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',
			$rawSection,
			$sectionInformations,
			PREG_SET_ORDER
		);

		
		//

		foreach ($sectionInformations as $sectionInformation)
		{
			$name = $sectionInformation[1];
			$value = array();

			for ($i = count($sectionInformation); $i > 2; $i--)
			{
				$value[] = $sectionInformation[$i - 1];
			}

			$result[$sectionName][] = array
			(
				'name' => $name,
				'value' => $value
			);
		}
	}


	return $result;
}

?>
